package com.android.phoenix.fragments.userinfoimprove;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.fragments.FragmentSwitchListener;

public class UserInfoSexFragment extends Fragment implements OnClickListener {
	private static final String TAG = "UserInfoSexFragment";

	/** 事件监听 */
	private FragmentSwitchListener listener;

	private Button btnUserInfoSexNext = null;
	private RadioButton rbUserInfoMale = null;
	private RadioButton rbUserInfoFemale = null;

	@Override
	public void onAttach(Activity activity) {
		try {
			listener = (FragmentSwitchListener) activity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fra_userinfosex, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		initViews();
	}

	/**
	 * @Title: initViews
	 * @Description: 控件初始化
	 * @author: WanChao
	 * @date: 2015-1-7 下午10:26:33
	 */
	private void initViews() {
		btnUserInfoSexNext = (Button) getActivity().findViewById(
				R.id.btnUserInfoSexNext);
		rbUserInfoMale = (RadioButton) getActivity().findViewById(
				R.id.rbUserInfoMale);
		rbUserInfoFemale = (RadioButton) getActivity().findViewById(
				R.id.rbUserInfoFemale);
		btnUserInfoSexNext.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnUserInfoSexNext:
			if (rbUserInfoMale.isChecked()) {
				saveUserSex(Constant.MALE);
			} else if (rbUserInfoFemale.isChecked()) {
				saveUserSex(Constant.FEMALE);
			} else {
				Toast toast = Toast.makeText(getActivity(), "请选择性别",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 300);
				toast.show();
				break;
			}
			listener.fragmentSwitch(UserInfoSexFragment.class.getName(),
					UserInfoAgeFragment.class.getName(), null);
			break;
		}
	}

	/**
	 * @Title: saveUserSex
	 * @Description: 保存用户性别信息
	 * @author: WanChao
	 * @date: 2015-1-16 下午10:36:52
	 */
	private void saveUserSex(int sex) {
		MainApplication.mSharedPreferences.edit().putInt(Constant.USERSEX, sex)
				.commit();
	}
}
